package com.dinzeer.legendreliclib;

import com.dinzeer.legendreliclib.configs.Config;
import com.dinzeer.legendreliclib.effect.LReffect;
import com.dinzeer.legendreliclib.event.SlashBladeCompat;
import com.dinzeer.legendreliclib.hud.RelicStackHUD;
import com.dinzeer.legendreliclib.lib.compat.slashblade.SlashBladeCompatEntities;
import com.dinzeer.legendreliclib.lib.key.SpecialTypeKeyMessage;
import com.dinzeer.legendreliclib.lib.util.dash.DashMessage;
import com.dinzeer.legendreliclib.lib.util.slashblade.AbstractSpecialEffect;
import com.mojang.logging.LogUtils;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.PackOutput;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import org.slf4j.Logger;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

// 主模组类，负责模组初始化和核心功能注册
@Mod(Legendreliclib.MODID)
@Mod.EventBusSubscriber
public class Legendreliclib {
    // 模组ID
    public static final String MODID = "legendreliclib";
    // 日志记录器
    private static final Logger LOGGER = LogUtils.getLogger();

    public static SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(MODID, "main"),
            () -> "1.0",
            s -> true,
            s -> true
    );
    private static int messageID = 0;







    private static final String PROTOCOL_VERSION = "1";
    public static final SimpleChannel PACKET_HANDLER = NetworkRegistry.newSimpleChannel(new ResourceLocation(MODID, MODID), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);
    public static <T> void addNetworkMessage(Class<T> messageType, BiConsumer<T, FriendlyByteBuf> encoder, Function<FriendlyByteBuf, T> decoder, BiConsumer<T, Supplier<NetworkEvent.Context>> messageConsumer) {
        PACKET_HANDLER.registerMessage(messageID, messageType, encoder, decoder, messageConsumer);
        messageID++;
    }

    public static ResourceLocation prefix(String path) {

        return new ResourceLocation(MODID, path);
    }


    // 模组构造函数
    public Legendreliclib() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        MinecraftForge.EVENT_BUS.register(this);

        modEventBus.addListener(this::onGatherData); // 添加数据生成事件监听

        modEventBus.addListener(this::onClientSetup);
        if (ModList.get().isLoaded("slashblade")){
            MinecraftForge.EVENT_BUS.register(SlashBladeCompat.class);
            MinecraftForge.EVENT_BUS.register(AbstractSpecialEffect.class);
            SlashBladeCompatEntities.ENTITIES.register(modEventBus);
        }
        ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC);
        LReffect.EFFECTS.register(modEventBus);


        INSTANCE.messageBuilder(DashMessage.class, messageID++)
                .encoder(DashMessage::encode)
                .decoder(DashMessage::decode)
                .consumerMainThread(DashMessage::handle)
                .add();
        INSTANCE.messageBuilder(SpecialTypeKeyMessage.class,messageID++)
                .encoder(SpecialTypeKeyMessage::encode)
                .decoder(SpecialTypeKeyMessage::decode)
                .consumerMainThread(SpecialTypeKeyMessage::handle)
                .add();
    }


    public void onClientSetup(final FMLClientSetupEvent event) {

        RelicStackHUD.register();
    }



    /**
     * 处理数据生成事件，注册数据生成器。
     *
     * @param event 数据生成事件
     */
    public void onGatherData(GatherDataEvent event) {
        DataGenerator generator = event.getGenerator();
        PackOutput packOutput = generator.getPackOutput();
        CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();
        ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
        // 注册物品标签生成器
//        generator.addProvider(event.includeServer(),
//                new ItemTagGenerator(packOutput, lookupProvider, MODID,existingFileHelper));

    }
}
